<?php

namespace App\Repositories;

use App\Models\Manager;
use App\Exceptions\InvalidException;
use Hash;
class ManagerRepo
{
    public function getAll()
    {
        return Manager::with('role')->get()->toArray();
    }
    public function create($data)
    {
        if(!empty($data['password'])) {
            $data['password'] = Hash::make($data['password'], [
                'rounds' => 12]);
        }
        return Manager::create($data);
    }
    public function getManager($account)
    {
        return Manager::where('account',$account)->first();
    }
    public function getManagerById($id)
    {
    	return Manager::where('id',$id)->first();
    }
    public function del($id)
    {
        $manager = Manager::where('id',$id)->first();
        if($manager) {
            $manager->delete();
        }  else {

        }
        return  true;
    }

    
    public function update($id,$data)
    {
        $manager = Manager::where('id',$id)->first();
        if(!$manager) {
            throw new InvalidException('管理员不存在');
        }
        $manager->name     = $data['name'];
        $manager->role_id  = $data['role_id'];
        $manager->email    = $data['email'];
        $manager->save();
    }
    public function editPassword($id,$password)
    {
        $manager = Manager::where('id',$id)->first();
        if(!$manager) {
            throw new InvalidException('管理员不存在');
        }
        $manager->password =Hash::make($password, [
                'rounds' => 12]);
        $manager->save();
    }
}